import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author ： peilinqian @date： 2024/10/28
 * @description: Optimize（300次）、dml（insert、update、delete各100次）两个线程并发执行
 **/

public class JDBC_Opengauss_Compatibility_Optimize_Case0005 {
	public static String url_str;
	public static String user_name;
	public static String user_pwd;
	public static String tb_name;
	public static void main(String[] args) throws InterruptedException, SQLException {
		if (args.length < 5) {
			System.out.print("need para: host:ip,db_b,user,password");
			return;
		}
		url_str = "jdbc:postgresql://" + args[0] + "/" + args[1] + "?LoggerLevel=INFO";
		user_name = args[2];
		user_pwd = args[3];
		tb_name = args[4];
		Connection connection = null;
		Statement statement = null;
		try {
			connection = DriverManager.getConnection(url_str, user_name, user_pwd);
			statement = connection.createStatement();
			statement.execute("drop table if exists " + tb_name + ";"
					+ "create table " + tb_name + "(c1 int,c2 text);");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		} finally {
			Thread s = new Optimize_Thread();
			s.start();
			Thread t = new Dml_Thread();
			t.start();
			t.join();
			s.join();
			try {
				statement.execute("drop table if exists " + tb_name);
			} catch (SQLException e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
			try {
				statement.close();
				connection.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}
	}
}


class Optimize_Thread extends Thread {
	public void run() {
		Statement statement = null;
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(JDBC_Opengauss_Compatibility_Optimize_Case0005.url_str,
					JDBC_Opengauss_Compatibility_Optimize_Case0005.user_name,
					JDBC_Opengauss_Compatibility_Optimize_Case0005.user_pwd);
			statement = connection.createStatement();
			for (int i = 0; i < 300; i++) {
				statement.execute("optimize table " + JDBC_Opengauss_Compatibility_Optimize_Case0005.tb_name);
				System.out.println("optimize 成功");
			}
			System.out.println("optimize线程完成");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			try {
				statement.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}

	}
}

class Dml_Thread extends Thread {
	public void run() {
		Connection connection = null;
		Statement statement = null;
		try {
			connection = DriverManager.getConnection(JDBC_Opengauss_Compatibility_Optimize_Case0005.url_str,
					JDBC_Opengauss_Compatibility_Optimize_Case0005.user_name,
					JDBC_Opengauss_Compatibility_Optimize_Case0005.user_pwd);
			statement = connection.createStatement();
			for (int i = 0; i < 100; i++) {
				try {
					statement.execute("insert into " + JDBC_Opengauss_Compatibility_Optimize_Case0005.tb_name
							+ " values(generate_series(1,100000),'a');");
					System.out.println("insert 成功");
				} catch (SQLException e) {
					System.out.println("insert 失败");
				}
				try {
					statement.execute(
							"delete from " + JDBC_Opengauss_Compatibility_Optimize_Case0005.tb_name + " where c1 > 5;");
					System.out.println("delete 成功");
				} catch (SQLException e) {
					System.out.println("delete 失败");
				}
				try {
					statement.execute(
							"update " + JDBC_Opengauss_Compatibility_Optimize_Case0005.tb_name + " set c2='new_a';");
					System.out.println("update 成功");
				} catch (SQLException e) {
					System.out.println("update 失败");
				}
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
			}
		}
	}
}
